{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 要点\n",
    "1. return_tensors: bool = True,  # 是否返回张量\n",
    "2. add_special_tokens: bool = True,  # 是否添加特殊token\n",
    "3. fast_tokenizer/slow_tokenizer: bool = True,  # 是否使用快速tokenizer\n",
    "4. `[sen] * 1000`进行批量操作"
   ],
   "id": "3813588d20610586"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "import warnings\n",
    "\n",
    "import transformers\n",
    "\n",
    "from MyHelper import *\n",
    "\n",
    "warnings.filterwarnings(\"ignore\")"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "sen = \"弱小的我也有大梦想\"\n",
    "tokenizer = transformers.AutoTokenizer.from_pretrained(Config.roberta_base_finetuned_dianping_chinese)"
   ],
   "id": "69bca5b9fa0de1a5",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "保存到本地 & 加载",
   "id": "2b17019982c3105d"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "tokenizer.save_pretrained(\"./tokenizer\")\n",
    "tokenizer = transformers.AutoTokenizer.from_pretrained(\"./tokenizer\")\n",
    "tokenizer, tokenizer.vocab_size"
   ],
   "id": "33899f74a4a5b967",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "古早方法",
   "id": "6b046684805d0775"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "tokens = tokenizer.tokenize(sen)\n",
    "token_ids = tokenizer.convert_tokens_to_ids(tokens)\n",
    "tokens2 = tokenizer.convert_ids_to_tokens(token_ids)\n",
    "text = tokenizer.convert_tokens_to_string(tokens2)\n",
    "tokens, token_ids, tokens2, text"
   ],
   "id": "6f7d11b15dfa59db",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "新方式",
   "id": "2d8cb889f7c56627"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "token_id_array = tokenizer.encode(sen, add_special_tokens=True, padding='max_length', truncation=True, max_length=15)\n",
    "text = tokenizer.decode(token_id_array)\n",
    "attention_mask = [int(n != 0) for n in token_id_array]\n",
    "token_id_array, text, attention_mask"
   ],
   "id": "ee62782dd0fcb12d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "单语句",
   "id": "5cb5ec4430763bff"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "inputs = tokenizer(sen, add_special_tokens=True, padding='max_length', truncation=True, max_length=15)\n",
    "inputs"
   ],
   "id": "378101e4e027d9c8",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "批量语句",
   "id": "3c7a53264fbfe12c"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "sens = [\"弱小的我也有大梦想\", \"我觉得不太行！\"]\n",
    "inputs = tokenizer(sens, add_special_tokens=True, padding='max_length', truncation=True, max_length=15)\n",
    "inputs, tokenizer.batch_decode(inputs[\"input_ids\"])"
   ],
   "id": "58be88b664de23a3",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "性能对比 use_fast true/false",
   "id": "42a5ed8baca54a2f"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "sen = \"弱小的我也有大dreaming\"\n",
    "loop = 10000\n",
    "fast_tokenizer = transformers.AutoTokenizer.from_pretrained(Config.roberta_base_finetuned_dianping_chinese, use_fast=True)\n",
    "slow_tokenizer = transformers.AutoTokenizer.from_pretrained(Config.roberta_base_finetuned_dianping_chinese, use_fast=False)"
   ],
   "id": "cbc72520cd2f6d34",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "%%time\n",
    "for i in range(loop):\n",
    "    fast_tokenizer(sen, add_special_tokens=True, padding='max_length', truncation=True, max_length=15)"
   ],
   "id": "a0eeb292fbb8d5bb",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "%%time\n",
    "for i in range(loop):\n",
    "    slow_tokenizer(sen, add_special_tokens=True, padding='max_length', truncation=True, max_length=15)"
   ],
   "id": "5e21016559156a9a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "%%time\n",
    "aa = fast_tokenizer([sen] * loop, add_special_tokens=True, padding='max_length', truncation=True, max_length=15)"
   ],
   "id": "320019158e6738f1",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "%%time\n",
    "aa = slow_tokenizer([sen] * loop, add_special_tokens=True, padding='max_length', truncation=True, max_length=15)"
   ],
   "id": "c75e5aab1166a1ce",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "inputs = fast_tokenizer(sen, add_special_tokens=True, padding='max_length', truncation=True, max_length=15, return_offsets_mapping=True)\n",
    "inputs, inputs.word_ids(), tokenizer.decode(inputs[\"input_ids\"]), \" \".join([tokenizer.decode(id) for id in inputs[\"input_ids\"]])"
   ],
   "id": "b28a2792f23213dc",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "e64007586405710a",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
